<?php

namespace app\api\controller;

use app\BaseController;
use frame\lib\Db;
use frame\lib\VerfiyParam;

class Account extends Base
{
    // 个人信息
    public function info()
    {
        $info = Db::name('user')->field('id,username,email,vip_time,score,create_time,last_login_time')->where(['id' => $this->uid])->find();
        $info['is_vip'] = getTime(3, $info['vip_time']) > time() ? true : false;
        success($info);
    }
    public function edit()
    {
        $param = $_POST;
        $rule = [
            'email|邮箱' => 'email',
            'password|密码' => 'min:6|max:12|alphaNum'
        ];
        VerfiyParam::init($param, $rule);
        $user = Db::name('user')->where(['id' => $this->uid])->find();
        if(isset($param['password']))
        {
            if(encrypt_pwd($param['password']) == $user['password'])  error('新密码不能与旧密码一致');
        }
        $data = [
            'email' => isset($param['email']) ? $param['email'] : $user['email'],
            'password' => isset($param['password']) ? encrypt_pwd($param['password']) : $user['password']
        ];
        $res = Db::name('user')->where(['id' => $this->uid])->update($data);
        if ($res) success();
        error();
    }
    // 购买商品的订单列表
    public function order()
    {
        $list = Db::name('good_order')->where(['uid' => $this->uid])->select();
        success($list);
    }
}
